using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco;
using FluidDemo.Model;
using Microsoft.Xna.Framework;
using WPCoco.Controllers;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Devices.Sensors;
using WPCoco.Graphics;

namespace FluidDemo.Scenes
{
    public class MainScene : Scene
    {
        public MainScene()
        {
            ControllerManager.Shared.TouchScreen.Enabled = true;

            layer = new SimulationLayer();

            if (DemoSettings.IsDevice)
            {
                AddNode(layer, -1);
            }
            else
            {
                AlphaTestNode wave = new AlphaTestNode(layer, true);
                wave.ContentScale = 0.4f;
                wave.ReferenceAlpha = 0.8f;
                AddNode(wave, -1);
            }
        }

        SimulationLayer layer;

        SpriteFont debugFont = Coco.Load<SpriteFont>("DebugFont");
        double frames = 0, elapsed = 0;
        string text = string.Empty;

        public override void SelfDraw(GameTime gameTime)
        {
            GraphicPrimitives.WorldMatrix = WorldMatrix.Shared.CurrentMatrix;
            for (int i = 0; i < layer.Environment.Entities.Count; i++)
            {
                IEntity obj = layer.Environment.Entities[i];
                switch (obj.Kind)
                {
                    case EntityKind.Circle:
                        Circle cobj = (Circle)obj;
                        GraphicPrimitives.AddFillCircle(cobj.Center, cobj.Radius, 0, 15, Color.Yellow);
                        GraphicPrimitives.AddFillCircle(cobj.Center, cobj.Radius - 3, 0, 15, Color.Green);
                        break;
                    case EntityKind.Atractor:
                        Atractor aobj = (Atractor)obj;
                        GraphicPrimitives.AddFillCircle(aobj.Center, aobj.Radius, 0, 15, Color.Lerp(Color.Red, Color.Transparent, 0.8f));
                        break;
                }
            }
            GraphicPrimitives.FlushFill();


            frames++;
            elapsed += gameTime.ElapsedGameTime.TotalSeconds;
            if (elapsed > 0.5)
            {
                text = string.Format("FPS {0}, Particles {1}, Collitions {2}", (int)(frames / elapsed), layer.Environment.Particles.Count, layer.Environment.Collitions);
                elapsed = 0;
                frames = 0;
            }

            CachedSpriteBatch batch = SpriteBatchCache.GetVolatile();

            batch.Begin();
            batch.DrawString(debugFont, text, new Vector2(10, Screen.Height - 30), Color.White);
            batch.End();
        }

    }
}
